package cn.buildoncloud.labor.workermanapplet.business.modules.services.impl;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.ProjectNotificationService;
import cn.buildoncloud.labor.workermanapplet.common.base.service.BaseServiceImpl;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import cn.buildoncloud.labor.workermanapplet.util.DateUtil;
import org.apache.http.client.utils.DateUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ProjectNotificationServiceImpl extends BaseServiceImpl<Map> implements ProjectNotificationService {

    public ProjectNotificationServiceImpl(Dao dao) {
        super(dao);
    }


    /**
     *@create 2019/1/14 17:25
     *@Param [pId, pageNumber, pageSize, search, sender, beginDate, endDate]
     *@return cn.buildoncloud.labor.workermanapplet.common.page.Pagination
     *@desc  项目通知  新加排序
     */
    @Override
    public Pagination listProjectNotification( Long pId,Integer pageNumber, Integer pageSize, String search, String sender, Date beginDate, Date endDate) {


        StringBuilder sb = new StringBuilder(" SELECT CONCAT(id,'') id, title, content, sender, show_tv, remark, senderIds,DATE_FORMAT(create_time,'%Y-%m-%d') create_time FROM bi_project_notification $condition");
        Sql sql = Sqls.create(sb.toString());
        Cnd cnd = Cnd.where("1","=",1);
        SqlExpressionGroup sqlExpressionGroup = new SqlExpressionGroup();
        sqlExpressionGroup.and("project_id","= ",pId);
        if (StringUtils.hasText(search)) sqlExpressionGroup.and("title","like ",search);
        if (StringUtils.hasText(sender)) sqlExpressionGroup.and("sender","like ",sender);
        if(beginDate!=null) sqlExpressionGroup.and("create_time",">=",beginDate);
        if (endDate != null){
            Calendar c = Calendar.getInstance();
            c.setTime(endDate);
            c.add(Calendar.DAY_OF_MONTH, 1);
            endDate = c.getTime();
            sqlExpressionGroup.and("create_time","<=",endDate);
        }

        cnd.and(sqlExpressionGroup);
        cnd.desc("id");

        sql.setCondition(cnd);
        return this.listPage(pageNumber,pageSize, sql);
    }

    @Override
    public Map<String, Object> getProjectNotification(Long id) {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(id,'') id, title, content, sender, show_tv, remark, senderIds, DATE_FORMAT(create_time,'%Y-%m-%d') create_time ");
        sb.append(" FROM bi_project_notification ");
        sb.append(" WHERE id = @id ");

        Sql sql = Sqls.create(sb.toString());
        sql.setParam("id", id);

        Record record = this.list(sql).get(0);
        String senderIds = record.getString("senderIds");
        if (StringUtils.hasText(senderIds)){
            String[] ids = senderIds.split(",");
            String sender = record.get("sender").toString();
            record.put("senderDate", senderDate(sender, ids));
        }
        return record;
    }

    private List<Map<String, Object>> senderDate(String sender, String[] senderIds){
        List<Map<String, Object>> senders = new ArrayList<>();

        switch (sender){
            case "WTL":
                for (int i = 0; i < senderIds.length; i++) {
                    StringBuilder sb2 = new StringBuilder("SELECT CONCAT(id,'') id, CONCAT(bwt.NAME,'(', team_leader_name ,')') name FROM bi_workerteam bwt WHERE id = @id");
                    Sql sql = Sqls.create(sb2.toString());
                    sql.setParam("id", senderIds[i]);
                    if (this.list(sql).size() > 0){
                        senders.add(this.list(sql).get(0));
                    }

                }
                break;
            case "WM":
                for (int i = 0; i < senderIds.length; i++) {
                    StringBuilder sb2 = new StringBuilder(" SELECT CONCAT(wt.id,'') id, CONCAT(wc.`name`,'-',wt.`name`) name ");
                    sb2.append(" FROM `sys_work_type` wt ");
                    sb2.append(" LEFT JOIN sys_work_class wc ON wt.work_class_id = wc.id ");
                    sb2.append(" WHERE wt.id = @id; ");
                    Sql sql = Sqls.create(sb2.toString());
                    sql.setParam("id", senderIds[i]);
                    if (this.list(sql).size() > 0){
                        senders.add(this.list(sql).get(0));
                    }
                }
                break;
        }
        return senders;
    }
}
